<?php
/**
 * Description of ConviteService
 *
 * @author Magno
 */
class ConviteService {


    private $conviteDAO;
    function __construct() {
        $this->conviteDAO = new ConviteDAO();
    }


    public function salvar($convite) {

        try {
            return $this->conviteDAO->salvar($convite);
        }catch(Exception $err) {
            throw new Exception($err->getMessage());
        }

    }
    
    public function salvarConvites($convites){
        $conexao = Conexao::getConexao();
        $conexao->beginTransaction();

        try {
            
            for ($i = 0; $i < count($convites); $i++) {
                $convites[$i] = $this->salvar($convites[$i]);
            }
        
        } catch (Exception $exc) {
            $conexao->rollBack();
            throw new Exception($exc->getMessage());
        }
        
        $conexao->commit();
        
        return $convites;
        
    }
    
    public function enviarConvites($convites){
        
        try {
            
            $emailService = new EmailService();
            for ($i = 0; $i < count($convites); $i++) {

                $convite = $convites[$i];
                $emailService->enviarEmail($convite->getEmail(), $convite->getMensagem());
                
            }
        
        } catch (Exception $exc) {            
            throw new Exception($exc->getMessage());
        }
        
    }

    public function excluir($conviteID) {

        try {
            if(!isset ($conviteID) || $conviteID <= 0)
                throw new Exception("Codigo da Convite Nao Econtrado!!!");

            $this->conviteDAO->excluir($conviteID);
        }catch(Exception $err) {
            throw new Exception($err->getMessage());
        }

    }

    public function listar($pagina = 0, $email = -1, $usuarioID = -1, $statusID = -1, $carregarDependencias = false) {
        try {
            return $this->conviteDAO->listar($pagina, $email, $usuarioID, $statusID, $carregarDependencias);
        }catch(Exception $err) {
            throw new Exception($err->getMessage());
        }
    }

    public function listarPorOrdem($pagina = 0, $tamanho = 0, $ordem = "conviteID", $direcao = "DESC", $carregarDependencias = false) {
        try {
            return $this->conviteDAO->listarPorOrdem($pagina, $tamanho, $ordem, $direcao, $carregarDependencias);
        }catch(Exception $err) {
            throw new Exception($err->getMessage());
        }
    }

    public function buscarPorID($conviteID, $carregarDependencias = false) {
        try {
            if(!isset ($conviteID) || $conviteID <= 0)
                throw new Exception("Codigo da Convite Nao Econtrado!!!");
            return $this->conviteDAO->buscarPorID($conviteID, $carregarDependencias);
        }catch(Exception $err) {
            throw new Exception($err->getMessage());
        }
    }

    public function total($email = -1, $usuarioID = -1, $statusID = -1) {
        try {
            return $this->conviteDAO->total($email, $usuarioID, $statusID);
        }catch(Exception $err) {
            throw new Exception($err->getMessage());
        }
    }
}
?>
